﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软构作业Winform
{
    public partial class Form5: Form
    {
        private Label lblTranslation;
        private TextBox txtInput;
        private Label lblResult;
        private Button btnNext;
        private string currentWord;
        // 数据库连接字符串
        private string connectionString = "Data Source=vocabulary.db;Version=3;";
        private SQLiteConnection connection;
        private SQLiteCommand command;
        public Form5()
        {
            InitializeComponent();
            InitializeUI();
            InitializeDatabase();
            LoadNextWord();
        }

        private void Form5_Load(object sender, EventArgs e)
        {

        }

        // 初始化数据库
        private void InitializeDatabase()
        {
            if (!File.Exists("vocabulary.db"))
            {
                SQLiteConnection.CreateFile("vocabulary.db");
                connection = new SQLiteConnection(connectionString);
                connection.Open();
                command = new SQLiteCommand(
                    @"CREATE TABLE Vocabulary (
                ID INTEGER PRIMARY KEY AUTOINCREMENT,
                Word TEXT NOT NULL,
                Translation TEXT NOT NULL)",
                    connection);
                command.ExecuteNonQuery();

                // 插入示例数据
                command.CommandText = "INSERT INTO Vocabulary (Word, Translation) VALUES " +
                                      "('apple', '苹果'), ('database', '数据库'), ('algorithm', '算法')";
                command.ExecuteNonQuery();
            }
            else
            {
                connection = new SQLiteConnection(connectionString);
                connection.Open();
            }
        }

        private void InitializeUI()
        {
            this.Text = "百词斩";
            this.Size = new Size(400, 300);

            // 中文
            lblTranslation = new Label { Location = new Point(50, 50), Width = 300 };
            // 输入框
            txtInput = new TextBox { Location = new Point(50, 100), Width = 200 };
            // 正误判断
            lblResult = new Label { Location = new Point(50, 150), Width = 1000, ForeColor = Color.Red };
            // 下一题
            btnNext = new Button { Text = "下一题", Location = new Point(50, 200) };

            // 绑定事件
            txtInput.KeyDown += TxtInput_KeyDown;
            btnNext.Click += BtnNext_Click;

            this.Controls.AddRange(new Control[] { lblTranslation, txtInput, lblResult, btnNext });
        }

        // 随机获取单词
        private void LoadNextWord()
        {
            using (var cmd = new SQLiteCommand(
                "SELECT Word, Translation FROM Vocabulary ORDER BY RANDOM() LIMIT 1",
                connection))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        currentWord = reader["Word"].ToString();
                        lblTranslation.Text = reader["Translation"].ToString();
                    }
                }
            }
            txtInput.Clear();
            lblResult.Text = "";
        }

        // 验证用户输入
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string userInput = txtInput.Text.Trim();
                bool isCorrect = userInput.Equals(currentWord, StringComparison.OrdinalIgnoreCase);
                lblResult.Text = isCorrect ? "正确" : "错误";
                e.Handled = true;
            }
        }

        // 下一题按钮事件
        private void BtnNext_Click(object sender, EventArgs e)
        {
            LoadNextWord();
        }
    }
}
